﻿using System;
using System.Web;

namespace RoketSolutions.SP2010.Infrostructure.UI.Ajax.Abstractions
{
    public class RequestContext
    {
        private readonly HttpContext _httpContext;

        public RequestContext(HttpContext context)
        {
            _httpContext = context;
        }

        #region / Properties /

        private HttpContextWrapper _httpContextWrapper;
        public virtual HttpContextWrapper HttpContext
        {
            get { return _httpContextWrapper ?? (_httpContextWrapper = new HttpContextWrapper(_httpContext)); }
            set { _httpContextWrapper = value; }
        }

        string _controllerName;
        public string ControllerName
        {
            get { return _controllerName ?? (_controllerName = ResolveControllerName(HttpContext.Request.Url)); }
        }

        string _actionName;
        public string ActionName
        {
            get { return _actionName ?? (_actionName = ResolveActionName(HttpContext.Request.Url)); }
        }

        #endregion

        #region / Methods /

        private static string ResolveControllerName(Uri url)
        {
            return url.Segments[url.Segments.Length - 2].Replace("/", "");
        }

        private static string ResolveActionName(Uri url)
        {
            return url.Segments[url.Segments.Length - 1];
        }

        #endregion

    }
}
